/*
617. 合并二叉树
 */

package LC0617;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution1 {

    // dei
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        TreeNode resultTree = new TreeNode();

        if (root1 == null || root2 == null){
            resultTree = (root1 == null ? root2 : root1);
            return resultTree;
        }

        if (root1 != null && root2 != null) {
            resultTree.val = root1.val + root2.val;
            resultTree.left = mergeTrees(root1.left, root2.left);
            resultTree.right = mergeTrees(root1.right, root2.right);
        }

        return resultTree;
    }
}
